<?php

namespace App\Http\Controllers\WorkOrder\Merchant;

use App\Http\Controllers\BaseController;
use App\Models\Appraise\AppraiseModel;
use App\Service\BaseService;
use App\Service\WorkOrder\Appraise\AppraiseService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;

class AppraiseController extends BaseController
{

    /**
     * 发布评价
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function add(Request $request): JsonResponse
    {
        Log::info('addDebug', ['request' => $request]);
        $this->validate(
            $request, [
                'score' => 'numeric|min:0|max:5',
                'type'  => 'required|integer',
            ]
        );
        $type = (int)$request->get('type');
        if (!in_array($type, AppraiseModel::NOT_ORDER_NO_MAPPING)) {
            $this->validate(
                $request, [
                    'workOrderNo' => 'required|string|min:0|max:255',
                ]
            );
        }
        $shopId = $request->header('shopId');
        $desc   = (string)$request->get('desc', '');
        $img    = $request->get('img', []);
        $tag    = $request->get('tag', []);
        $mobile = (string)$request->get('mobile', '');

        $currentUid = getRmsUserId();
        AppraiseService::add($request, $shopId, $currentUid, $desc, $img, $tag, $mobile);
        return $this->success();
    }

    /**
     * 获取评价对应内容配置
     * @param Request $request
     * @return JsonResponse
     */
    public function config(Request $request): JsonResponse
    {
        $type = (int)$request->get('type', 1);
        return $this->success(AppraiseService::getConfig($type));
    }

    /**
     * 查看评价详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function info(Request $request, int $id): JsonResponse
    {
        return $this->success(AppraiseService::info($id, 2));
    }


    /**
     * 获取评价对应内容配置-new
     * @param Request $request
     * @return JsonResponse
     */
    public function getNewConfig(Request $request): JsonResponse
    {
        return $this->success(AppraiseService::getNewConfig());
    }

    /**
     * 查看评价详情-通过订单号和类型
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function getInfoByOrderNo(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'workOrderNo' => 'required|string|min:0|max:255',
                'type'        => 'required|integer',
            ]
        );
        return $this->success(AppraiseService::info(0, 2, $request->workOrderNo, $request->type));
    }
}
